package ui;

import baidu.SpeechBaidu;
import ctrl.UICtrl;
import opencv.FaceCv;
import util.DataUtil;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

@SuppressWarnings("serial")
public class MainUI extends JFrame implements KeyListener, CaretListener {

    //组件声明
    private JScrollPane jsp;
    private JTextArea jta = new JTextArea();

    //变量申明
    private int currentKeyCode;
    private UICtrl uiCtrl;

    public MainUI() {
        //问好
        SpeechBaidu.speak(DataUtil.getData());

        //获取宽高
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int width = dim.width;
        int height = dim.height;

        //配置布局
        this.setLayout(null);

        //背景配置
        final ImageIcon icon = new ImageIcon("img/background.jpg");
        JPanel backdrop = new JPanel() {
            private static final long serialVersionUID = 1957203784117943458L;

            {
                this.setOpaque(false);
                this.setLayout(new BorderLayout());
            }

            public void paintComponent(Graphics g) {
                g.drawImage(icon.getImage(), 0, 0, this);
                super.paintComponents(g);
            }
        };
        backdrop.setBounds(0, 0, width / 2, height / 2);

        uiCtrl = new UICtrl(jta);

        //组件配置
        jta.setOpaque(false);
        jta.setCursor(new Cursor(Cursor.TEXT_CURSOR));

        //换行处理
        uiCtrl.input();

        //事件监听
        jta.addKeyListener(this);
        jta.addCaretListener(this);
        jta.setFont(new Font("宋体", Font.HANGING_BASELINE, 14));

        //面板设置
        jsp = new JScrollPane(jta);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jsp.setBackground(new Color(Color.TRANSLUCENT));
        jsp.setOpaque(false);
        jsp.getViewport().setOpaque(false);

        //添加组件
        backdrop.add(jsp);
        this.add(backdrop);

        //窗口配置
        this.setResizable(false);
        this.setOpacity(0.9f);
        this.setVisible(false); //设置显示
        this.setTitle("Aibaby"); //设置标题
        this.setBounds(width / 4, height / 4 - 20, width / 2 + 5, height / 2 + 30); //窗口大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setIconImage(new ImageIcon("img/system.png").getImage());
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        new FaceCv().videoFace();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource() == jta) {
            //回车监听
            if (currentKeyCode == KeyEvent.VK_ENTER) {
                uiCtrl.mutual();
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        currentKeyCode = e.getKeyCode();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void caretUpdate(CaretEvent e) {
        // TODO Auto-generated method stub

    }
}
